flutterSdkDir=$1
echo "flutter sdk dir is ${flutterSdkDir}"

echo '删除flutter/assets目录'
rm -rf assets
rm -rf libs

echo "goto flutter project menu"
cd ..
cd ..

echo "Clean old build"
find . -d -name "build" | xargs rm -rf
flutter clean

echo "Get packages"
flutter packages get

echo "Build release AOT"
flutter build aot --suppress-analytics --target=lib/main.dart --target-platform=android-arm --output-dir=build/flutteroutput/aot --build-shared-library --release

echo "Build release Bundle"
flutter build bundle --suppress-analytics --target=lib/main.dart --target-platform=android-arm --precompiled  --asset-dir=build/flutteroutput/flutter_assets --release

echo 'Create Flutter input(flutter build)'
mkdir .android/flutter/assets
mkdir -p .android/flutter/libs/lib/armeabi

# 拷贝asset
#echo 'Copy vm_snapshot_data'
#cp -r -f build/flutteroutput/aot/vm_snapshot_data .android/flutter/assets
#echo 'Copy vm_snapshot_instr'
#cp -r -f build/flutteroutput/aot/vm_snapshot_instr .android/flutter/assets
#echo 'Copy isolate_snapshot_data'
#cp -r -f build/flutteroutput/aot/isolate_snapshot_data .android/flutter/assets
#echo 'Copy isolate_snapshot_instr'
#cp -r -f build/flutteroutput/aot/isolate_snapshot_instr .android/flutter/assets
echo 'Copy flutter_assets'
flutterAssetsFile=".android/flutter/assets/flutter_assets"
cp -r -f build/flutteroutput/flutter_assets .android/flutter/assets

echo 'Copy app.so'
cp -r -f build/flutteroutput/aot/app.so .android/flutter/libs/lib/armeabi/libapp.so

#echo 'Copy flutter_assets if not exist'
#flutterAssetsFile=".android/flutter/assets/flutter_assets"
#if [ ! -f "$flutterAssetsFile" ]; then
#echo 'Copy flutter_assets is not exist'
#cp -r -f build/flutteroutput/flutter_assets .android/flutter/assets
#fi

echo 'Copy flutter.jar if not exist'
myFile=".android/flutter/libs/flutter.jar"
# 这里的-f参数判断$myFile是否存在
if [ ! -f "$myFile" ]; then
echo 'flutter.jar is not exist'
# 拷贝flutter.jar
echo 'Copy flutter jar'
mkdir -p .android/flutter/libs && cp ${flutterSdkDir}/bin/cache/artifacts/engine/android-arm-release/flutter.jar "$_"

# 将arm-v7中的so拷贝然后创建复制到armabi
echo '将arm-v7中的so拷贝然后创建复制到armabi'
echo 'handle .so in flutter.jar'
cd .android/flutter/libs
#cp flutter.jar flutter-armeabi-v7a.jar # 备份
unzip flutter.jar lib/armeabi-v7a/libflutter.so
mv lib/armeabi-v7a/libflutter.so lib/armeabi
zip -d flutter.jar lib/armeabi-v7a/libflutter.so
zip flutter.jar lib/armeabi/libflutter.so
zip flutter.jar lib/armeabi/libapp.so

#echo 'Copy flutter_shared'
#unzip flutter.jar assets/flutter_shared/*.dat -d lib/
#mv lib/assets/flutter_shared ../assets

rm -rf lib
fi